package com.atgion.manager.service.impl;

import com.atgion.manager.mapper.TagMapper;
import com.atgion.manager.service.AdminTagService;
import com.atgion.spzx.model.bo.blog.TagDO;
import com.atgion.spzx.model.vo.base.Result;
import com.atgion.spzx.model.vo.blog.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-04-17 12:08
 * @description: TODO
 **/
@Service
@Slf4j
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements AdminTagService {
    @Autowired
    private TagMapper tagMapper;


    @Override
    public Result addTags(AddTagReqVO addTagReqVO) {
        List<TagDO> tags = addTagReqVO.getTags().stream()
                .map(tagName -> TagDO.builder().name(tagName.trim())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build()).collect(Collectors.toList());
        try {
            saveBatch(tags);
        } catch (DuplicateKeyException e) {
            log.error("==> 含有重复标签名", e);
            return Result.failed("含有重复标签名");
        }
        return Result.success();
    }

    @Override
    public Result queryTagPageList(QueryTagPageListReqVO queryTagPageListReqVO) {
        Long current = queryTagPageListReqVO.getCurrent();
        Long size = queryTagPageListReqVO.getSize();
        LocalDateTime startDate = queryTagPageListReqVO.getStartDate();
        LocalDateTime endDate = queryTagPageListReqVO.getEndDate();
        String tagName = queryTagPageListReqVO.getTagName();

        Page<TagDO> page = new Page<>(current, size);
        QueryWrapper<TagDO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(Objects.nonNull(tagName), TagDO::getName, tagName)
                .ge(Objects.nonNull(startDate), TagDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), TagDO::getCreateTime, endDate)
                .orderByDesc(TagDO::getCreateTime);

        Page<TagDO> tagDOPage =  tagMapper.selectPage(page, wrapper);

        return Result.success(tagDOPage);
    }

    @Override
    public Result deleteTag(DeleteTagReqVO deleteTagReqVO) {
        removeById(deleteTagReqVO.getTagId());
        return Result.success();
    }

    @Override
    public Result searchTags(SearchTagReqVO searchTagReqVO) {
        String key = searchTagReqVO.getKey();

        QueryWrapper<TagDO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .apply("NAME like UPPER(CONCAT('%', {0}, '%')) OR NAME LIKE LOWER(CONCAT('%', {0}, '%'))", key)
                .orderByAsc(TagDO::getName);
        List<TagDO> tagDOS = tagMapper.selectList(wrapper);
        List<QuerySelectListRspVO> selectListRspVOS = null;

        if (!CollectionUtils.isEmpty(tagDOS)) {
            selectListRspVOS = tagDOS.stream()
                    .map(p -> QuerySelectListRspVO.builder().label(p.getName()).value(p.getId()).build())
                    .collect(Collectors.toList());
        }
        return Result.success(selectListRspVOS);
    }

    @Override
    public Result queryTagSelectList() {
        List<TagDO> tagDOS = tagMapper.selectList(null);
        List<QuerySelectListRspVO> list = null;
        if (!CollectionUtils.isEmpty(tagDOS)) {
            list = tagDOS.stream()
                    .map(p -> QuerySelectListRspVO.builder()
                            .label(p.getName())
                            .value(p.getId())
                            .build())
                    .collect(Collectors.toList());
        }
        return Result.success(list);
    }
}
